/*
 * @lc app=leetcode.cn id=653 lang=typescript
 *
 * [653] 两数之和 IV - 输入 BST
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

// TODO Hash 表加树遍历
function findTarget(root: TreeNode | null, k: number): boolean {
    if (!root) return false;
    let set = new Set<number>();

    const tra = (root: TreeNode | null): boolean => {
        if (!root) return false;
        if (set.has(k - root.val)) return true;
        set.add(root.val);
        return tra(root.left) || tra(root.right);
    };
    return tra(root);
}

// @lc code=end
